package com.recruit.index;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jeecg.recruitcompanyinfo.entity.RecruitCompanyInfoEntity;
import com.jeecg.recruitcompanyjob.entity.RecruitCompanyJobEntity;
import com.jeecg.recruitcompanyjob.service.RecruitCompanyJobServiceI;

@Controller
@RequestMapping("/indexContoller")
public class IndexController {
	
	@Autowired
	private RecruitCompanyJobServiceI recruitCompanyJobService;
	
	/**
	 * 跳转到网站首页
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "toIndex")
	public ModelAndView toIndex(HttpSession session,HttpServletRequest request,ModelMap modelMap) {
		//随机获取4条热门岗位
		String hql = "from RecruitCompanyJobEntity order by RAND()";
		List<RecruitCompanyJobEntity> recruitCompanyJobEntity = recruitCompanyJobService.findByQueryString(hql);
		List<IndexVo> hotJob = new ArrayList<>();
		IndexVo indexVo;
		if(recruitCompanyJobEntity.size()>4){
			for (int i=0;i<4;i++ ) {
				RecruitCompanyJobEntity job = new RecruitCompanyJobEntity();
				BeanUtils.copyProperties(recruitCompanyJobEntity.get(i), job);
				job.setEducation(TypeUtil.getEduName(job.getEducation()));
				
				RecruitCompanyInfoEntity company = recruitCompanyJobService.findUniqueByProperty(RecruitCompanyInfoEntity.class, "userId", job.getCompanyId());
				RecruitCompanyInfoEntity companyVo = new RecruitCompanyInfoEntity();
				BeanUtils.copyProperties(company, companyVo);
				companyVo.setDevelopState(TypeUtil.getDevelopState(company.getDevelopState()));
				indexVo = new IndexVo();
				indexVo.setJob(job);
				indexVo.setCompany(companyVo);
				hotJob.add(indexVo);
			}
		}else{
			for (RecruitCompanyJobEntity recruitCompanyJobEntity2 : recruitCompanyJobEntity) {
				RecruitCompanyJobEntity job = new RecruitCompanyJobEntity();
				BeanUtils.copyProperties(recruitCompanyJobEntity2, job);
				job.setEducation(TypeUtil.getEduName(job.getEducation()));
				RecruitCompanyInfoEntity company = recruitCompanyJobService.findUniqueByProperty(RecruitCompanyInfoEntity.class, "userId", job.getCompanyId());
				RecruitCompanyInfoEntity companyVo = new RecruitCompanyInfoEntity();
				BeanUtils.copyProperties(company, companyVo);
				companyVo.setDevelopState(TypeUtil.getDevelopState(company.getDevelopState()));
				indexVo = new IndexVo();
				indexVo.setJob(job);
				indexVo.setCompany(companyVo);
				hotJob.add(indexVo);
			}
		}
		
		//随机获取4个热门公司
		String hql2 = "from RecruitCompanyInfoEntity order by RAND()";
		List<RecruitCompanyInfoEntity> recruitCompanyInfoEntity = recruitCompanyJobService.findByQueryString(hql2);
		List<RecruitCompanyInfoEntity> hotCompany = new ArrayList<>();
		if(recruitCompanyInfoEntity.size()>4){
			for (int i=0;i<4;i++ ) {
				RecruitCompanyInfoEntity companyVo = new RecruitCompanyInfoEntity();
				BeanUtils.copyProperties(recruitCompanyInfoEntity.get(i), companyVo);
				companyVo.setDevelopState(TypeUtil.getDevelopState(companyVo.getDevelopState()));
				hotCompany.add(companyVo);
			}
		}else{
			for (RecruitCompanyInfoEntity recruitCompanyInfoEntity2 : recruitCompanyInfoEntity) {
				RecruitCompanyInfoEntity companyVo = new RecruitCompanyInfoEntity();
				BeanUtils.copyProperties(recruitCompanyInfoEntity2, companyVo);
				companyVo.setDevelopState(TypeUtil.getDevelopState(recruitCompanyInfoEntity2.getDevelopState()));
				hotCompany.add(companyVo);
			}
		}
		
		modelMap.addAttribute("hotJob", hotJob);
		modelMap.addAttribute("hotCompany", hotCompany);
		
		
		return new ModelAndView("com/recruit/index/index");
	}
	
	

}
